﻿using UnityEngine;

namespace Yoing945
{
    public static class TransformExtensions
    {
        /// <summary>
        /// 重置 localPosition, localScale, localRotation
        /// </summary>
        /// <param name="transform"></param>
        public static void ResetLocal(this Transform transform)
        {
            transform.localPosition = Vector3.zero;
            transform.localScale = Vector3.one;
            transform.localRotation = Quaternion.identity;
        }

        /// <summary>
        /// 移除所有子物体
        /// </summary>
        /// <param name="t"></param>
        public static void RemoveChildren(this Transform t)
        {
            if (t == null)
                return;
            while (t.childCount != 0)
            {
                Transform child = t.GetChild(0);

                if (Application.isPlaying)
                {
                    child.SetParent(null);
                    Object.Destroy(child.gameObject);
                }
                else
                {
                    Object.DestroyImmediate(child.gameObject);
                }
            }
        }
    }
}